home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Magazine / C_Tutorial / Part-11 / req0 / main.c < prev    next >
C/C++ Source or Header  |  1998-03-02  |  6KB  |  274 lines

  1. #include<dos/dos.h>
  2. #include<dos/rdargs.h>
  3. #include<exec/libraries.h>
  4. #include<workbench/startup.h>
  5. #include<workbench/workbench.h>
  6.  
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9.  
  10. #include<clib/dos_protos.h>
  11. #include<clib/exec_protos.h>
  12. #include<clib/icon_protos.h>
  13.  
  14. #include "main.h"
  15. #include "fractal.h"
  16. #include "gui.h"
  17. #include "idcmp.h"
  18. #include "loadsave.h"
  19. #include "arexx.h"
  20.  
  21. /* The library base global variables */
  22. /* (The different style of opening libraries requires these to be initialised to NULL) */
  23. struct Library* GfxBase = NULL;
  24. struct Library* IntuitionBase = NULL;
  25. struct Library* GadToolsBase = NULL;
  26. struct Library* AslBase = NULL;
  27. struct Library* DosBase = NULL;
  28. struct Library* IFFBase = NULL;
  29. struct Library* RexxSysBase = NULL;
  30. struct Library* IconBase = NULL;
  31.  
  32. /* Need to give prototypes for our functions */
  33. static int  createAll(struct WBStartup*, char*);
  34. static void freeAll(void);
  35. static int  openLibs(void);
  36. static void closeLibs(void);
  37.  
  38. /* The alternative "main()" for Workbench startup */
  39. void wbmain(struct WBStartup*);
  40.  
  41. /* The shared starting point of our program */
  42. static void realmain(struct WBStartup*, char*);
  43.  
  44. #define TT_DEPTH       "DEPTH"
  45. #define TT_PORTNAME    "PORTNAME"
  46.  
  47. #define ARGS_TEMPLATE  "FILE," TT_DEPTH "/N," TT_PORTNAME
  48.  
  49. enum ARGS { ARG_FILENAME, ARG_DEPTH, ARG_PORTNAME, NUM_ARGS };
  50.  
  51. #define DEFAULT_PORTNAME "HELLOPAINTER"
  52. #define DEFAULT_DEPTH    (4)
  53.  
  54. /* A place to store our program name for the default tool */
  55. static char progname[MAXFILENAME];
  56.  
  57. char* progName()
  58. {
  59.     return progname;
  60. }
  61.  
  62. static void setProgName(struct WBStartup* wbmsg, char* prog)
  63. {
  64.     BPTR dir;
  65.   char* file;
  66.   if(wbmsg)
  67.     {
  68.         dir = wbmsg->sm_ArgList[0].wa_Lock;
  69.         file = wbmsg->sm_ArgList[0].wa_Name;
  70.     }
  71.     else
  72.     {
  73.         dir = Lock("PROGDIR:", ACCESS_READ);
  74.         file = prog;
  75.     }
  76.     *progname = '\0';
  77.     NameFromLock(dir, progname, MAXFILENAME);
  78.     AddPart(progname, file, MAXFILENAME);
  79.   if(prog && dir)
  80.         UnLock(dir);
  81. }
  82.  
  83. /* The CLI starting point for StormC, but the general start for SAS/C */
  84. void main(int argc, char** argv)
  85. {
  86.     /* argc should never be zero: SAS/C uses this to indicate WB start */
  87.   if(argc == 0)
  88.         wbmain((struct WBStartup*)argv);
  89.     else
  90.         realmain(NULL, argv[0]);
  91. }
  92.  
  93. /* The WB starting point for StormC */
  94. void wbmain(struct WBStartup* wbmsg)
  95. {
  96.     /* WB-specific startup could go here */
  97.     realmain(wbmsg, NULL);
  98. }
  99.  
  100. /* The start of the program */
  101. static void realmain(struct WBStartup* wbmsg, char* prog)
  102. {
  103.     if(createAll(wbmsg, prog))
  104.         handleIDCMP();
  105.     freeAll();
  106. }
  107.  
  108. static struct RDArgs* rdargs = NULL;
  109. static struct DiskObject* dobj = NULL;
  110.  
  111. static int createAll(struct WBStartup* wbmsg, char* prog)
  112. {
  113.     int success = FALSE;
  114.   if(openLibs())
  115.     {
  116.         /* We'll only set portname if successful */
  117.         char* portname = NULL;
  118.         UBYTE depth;
  119.         char* filename = NULL;
  120.         BPTR currdir = NULL;
  121.         setProgName(wbmsg, prog);
  122.         initSemaphores();
  123.         if(wbmsg)
  124.         {
  125.             /* WB bit, use Tool Types */
  126.             currdir = CurrentDir(wbmsg->sm_ArgList[0].wa_Lock);
  127.             if(dobj = GetDiskObject(wbmsg->sm_ArgList[0].wa_Name))
  128.             {
  129.                 UBYTE** tt = (UBYTE**)dobj->do_ToolTypes;
  130.                 char* depthptr = FindToolType(tt, TT_DEPTH);
  131.                 portname = FindToolType(tt, TT_PORTNAME);
  132.                 /* Use the default if a Tool Type was not specified */
  133.                 if(portname == NULL)
  134.                     portname = DEFAULT_PORTNAME;
  135.                 if(depthptr)
  136.                     depth = atoi(depthptr);
  137.                 else
  138.                     depth = DEFAULT_DEPTH;
  139.             }
  140.             else
  141.                 printf("Error: could not read Tool Types\n");
  142.             /* Reset current directory, if we moved it */
  143.             if(currdir)
  144.             {
  145.                 CurrentDir(currdir);
  146.                 currdir = NULL;
  147.             }
  148.             if(wbmsg->sm_NumArgs > 1)
  149.             {
  150.                 currdir = CurrentDir(wbmsg->sm_ArgList[1].wa_Lock);
  151.                 filename = wbmsg->sm_ArgList[1].wa_Name;
  152.             }
  153.         }
  154.         else
  155.         {
  156.             /* CLI bit, use ReadArgs() */
  157.             LONG args[NUM_ARGS];
  158.             int i;
  159.             /* Initialise our args to NULL */
  160.             /* (This way we will know if an argument was specified) */
  161.             for(i=0; i<NUM_ARGS; i++)
  162.                 args[i] = NULL;
  163.             if(rdargs = ReadArgs(ARGS_TEMPLATE, args, NULL))
  164.             {
  165.                 LONG* depthptr = (LONG*)(args[ARG_DEPTH]);
  166.                 portname = (char*)(args[ARG_PORTNAME]);
  167.                 filename = (char*)(args[ARG_FILENAME]);
  168.                 /* Use the default if an argument was not specified */
  169.                 if(portname == NULL)
  170.                     portname = DEFAULT_PORTNAME;
  171.                 if(depthptr == NULL)
  172.                     depth = DEFAULT_DEPTH;
  173.                 else
  174.                     depth = (UBYTE)(*depthptr);
  175.             }
  176.             else
  177.                 printf("Error: could not read arguments\n");
  178.         }
  179.         if(portname)
  180.         {
  181.             if(createARexxPort(portname) && createArgs() && openGUI(depth,0,0,0))
  182.             {
  183.                 success = TRUE;
  184.                 if(filename)
  185.                     loadfile(filename);
  186.             }
  187.         }
  188.         /* Reset current directory, if we moved it */
  189.         if(currdir)
  190.             CurrentDir(currdir);
  191.     }
  192.     return success;
  193. }
  194.  
  195. static void freeAll()
  196. {
  197.     freeReqs();
  198.     closeGUI();
  199.     freeArgs();
  200.     freeARexxPort();
  201.     if(rdargs)
  202.         FreeArgs(rdargs);
  203.     if(dobj)
  204.         FreeDiskObject(dobj);
  205.     closeLibs();
  206. }
  207.  
  208. /* Try to open all the libraries -- return TRUE on success */
  209. static int openLibs()
  210. {
  211.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  212.     {
  213.         printf("Error: could not open graphics.library\n");
  214.         return FALSE;
  215.     }
  216.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  217.     {
  218.         printf("Error: could not open intuition.library\n");
  219.         return FALSE;
  220.     }
  221.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  222.     {
  223.         printf("Error: could not open gadtools.library\n");
  224.         return FALSE;
  225.     }
  226.     if((AslBase = OpenLibrary("asl.library",37)) == NULL)
  227.     {
  228.         printf("Error: could not open asl.library\n");
  229.         return FALSE;
  230.     }
  231.     if((DosBase = OpenLibrary("dos.library",37)) == NULL)
  232.     {
  233.         printf("Error: could not open dos.library\n");
  234.         return FALSE;
  235.     }
  236.     if((IFFBase = OpenLibrary("iff.library",23)) == NULL)
  237.     {
  238.         printf("Error: could not open iff.library\n");
  239.         return FALSE;
  240.     }
  241.     if((RexxSysBase = OpenLibrary("rexxsyslib.library",35)) == NULL)
  242.     {
  243.         printf("Error: could not open rexxsyslib.library\n");
  244.         return FALSE;
  245.     }
  246.     if((IconBase = OpenLibrary("icon.library",37)) == NULL)
  247.     {
  248.         printf("Error: could not open icon.library\n");
  249.         return FALSE;
  250.     }
  251.   return TRUE;
  252. }
  253.  
  254. /* Close any open library */
  255. static void closeLibs()
  256. {
  257.     if(IconBase)
  258.         CloseLibrary(IconBase);
  259.     if(RexxSysBase)
  260.         CloseLibrary(RexxSysBase);
  261.     if(IFFBase)
  262.         CloseLibrary(IFFBase);
  263.     if(DosBase)
  264.         CloseLibrary(DosBase);
  265.     if(AslBase)
  266.         CloseLibrary(AslBase);
  267.     if(GadToolsBase)
  268.         CloseLibrary(GadToolsBase);
  269.     if(IntuitionBase)
  270.         CloseLibrary(IntuitionBase);
  271.     if(GfxBase)
  272.         CloseLibrary(GfxBase);
  273. }
  274.